/**
 * Copyright 2021 xieyuanhui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.milantiejiang.tuto;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.net.Socket;

public class MainActivity extends AppCompatActivity implements IRspListener {
    private final String TAG = "MainActivity";
    private TextView show_msg_tv;
    private TextView msg_et;
    private Button send_msg_btn;
    private Button send_file_btn;
    private Button download_file_from_server_btn;
    public static String path;
    private ISendMsg mSendMsg=new SendMsgImpl();
    private MainHandler mMainHandler=new MainHandler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        path = getFilesDir().getAbsolutePath() +"/";
        initServer();
        show_msg_tv=findViewById(R.id.show_msg_tv);
        msg_et=findViewById(R.id.msg_et);
        send_msg_btn=findViewById(R.id.send_msg_btn);
        send_file_btn=findViewById(R.id.send_file_btn);
        download_file_from_server_btn=findViewById(R.id.download_file_from_server_btn);
        send_msg_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msg=msg_et.getText().toString();
                if(msg!=null&&!msg.equals("")){
                    mSendMsg.sendMsg(MainActivity.this,msg);
                    msg_et.setText("");
                }
            }
        });
        send_file_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSendMsg.sendFile(MainActivity.this,path+"clientFile.txt");
            }
        });
        download_file_from_server_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSendMsg.reqDowloadFile(MainActivity.this);
            }
        });
    }

    private void initServer(){
        SocketService socketService = new SocketService();
        socketService.startService();
    }

    @Override
    public void onTimeOut() {
        Message message=new Message();
        message.what=100;
        Bundle bundle=new Bundle();
        bundle.putString("msg","超时");
        message.setData(bundle);
        mMainHandler.sendMessage(message);
    }

    @Override
    public void onRspMsg(Socket socket, String msg) {
        Message message=new Message();
        message.what=100;
        Bundle bundle=new Bundle();
        bundle.putString("msg",msg);
        message.setData(bundle);
        mMainHandler.sendMessage(message);
    }

    public class MainHandler extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 100:
                    String str=msg.getData().getString("msg");
                    show_msg_tv.setText(str);
                    break;
                default:
                    break;
            }
        }
    }
}